using System;
using System.IO;
using CommandSystem;

namespace RgCore.Modules.Commands.SubPluginManagerCommands
{
    public class Load<T> : ICommand where T : Attribute, IName
    {
        public Load(string commandHead, SubPluginManager<T> manager)
        {
            CommandHead = commandHead;
            Manager = manager;
        }

        public string CommandHead { get; }
        public SubPluginManager<T> Manager { get; private set; }

        public string GetUsage() => $"用法: {Command} load <文件名>";

        public bool Execute(ArraySegment<string> arguments, ICommandSender sender, out string response)
        {
            if (!sender.CheckPermission(PlayerPermissions.ServerConsoleCommands))
            {
                response = "你没有权限执行此命令";
                return false;
            }

            if (arguments.Count < 1)
            {
                response = GetUsage();
                return false;
            }

            if (File.Exists(Path.Combine(Manager.FolderPath, $"{arguments.At(0)}.dll")))
            {
                Manager.LoadObject(Path.Combine(Manager.FolderPath, $"{arguments.At(0)}.dll"), true, out _);
                response = "加载成功！";
                return true;
            }

            response = "未找到此文件！";
            return false;
        }

        public string Command { get; } = "load";
        public string[] Aliases { get; } = Array.Empty<string>();
        public string Description { get; } = "加载子项目";
    }
}